package com.uuah.server.transport.retry.notifiers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.uuah.server.UuahServer;
import com.uuah.server.api.transport.retry.IRetryContext;
import com.uuah.server.api.transport.retry.IRetryNotifier;
import com.uuah.server.context.notification.ConnectionNotification;

/**
 *
 * TODO comment ...
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午02:47:17
 * @version 1.0.0
 */
public class ConnectNotifier implements IRetryNotifier {
	protected transient final Logger logger = LoggerFactory
			.getLogger(ConnectNotifier.class);

	public void sucess(IRetryContext context) {
		if (logger.isDebugEnabled()) {
			logger.debug("Successfully connected to "
					+ context.getDescription());
		}

		fireConnectNotification(ConnectionNotification.CONNECTION_CONNECTED,
				context.getDescription());
	}

	public void failed(IRetryContext context, Throwable e) {
		fireConnectNotification(ConnectionNotification.CONNECTION_FAILED,
				context.getDescription());

		if (logger.isErrorEnabled()) {
			StringBuffer msg = new StringBuffer(512);
			msg.append("Failed to connect/reconnect: ").append(
					context.getDescription());
			msg.append(". Exception was: ").append(e);
			logger.error(msg.toString());
		}
	}

	protected void fireConnectNotification(int action, String description) {
		UuahServer.getUuahContext().fireNotification(
				new ConnectionNotification(null, description, action));
	}
}
